package com.home.duy.code.Model;

import java.text.DateFormat;
import java.util.List;
import java.util.Vector;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.home.Entity.CaLamViec;
import com.home.duy.code.DAO.CaLamViecDAO;

public class QLCaLamTableModel extends AbstractTableModel implements TableModelListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] column = { "Tên Ca Làm" ,"TG Bắt Đầu","TG Kết Thúc","Ghi Chú"};


	public QLCaLamTableModel() {
	}

	@Override
	public int getColumnCount() {
		return column.length;
	}

	@Override
	public int getRowCount() {
		return getData().size();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (getData().size() > 0)
			return ((Vector) getData().get(rowIndex)).get(columnIndex);
		else
			return "";
	}

	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return this.column[column];
	}

	@Override
	public Class<?> getColumnClass(int arg0) {
		return getValueAt(0, arg0).getClass();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private Vector getData() {
		Vector<Object> v = new Vector<Object>();
		CaLamViecDAO clDao = new CaLamViecDAO();
		List<CaLamViec> caLams = clDao.findAll(CaLamViec.class);
		for (CaLamViec cl : caLams) {
				Vector<Object> column = new Vector<Object>();
				column.add(cl.getTen());
				DateFormat df;
				df = DateFormat.getTimeInstance();
				column.add(df.format(cl.getTgBd()));
				column.add(df.format(cl.getTgKt()));
				column.add(cl.getGhiChu());
				column.add(cl.getMaSo());
				v.add(column);
			}
		return v;
	}

	@Override
	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub

	}
}
